﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm.Middle_1468_Convert
{
    class MainProject
    {
        static void Main(string[] args)
        {
            //string s = Convert("PAYPALISHIRING",3);
            string s = Convert("AB", 1);
            Console.WriteLine(s);

            Console.ReadKey();
        }
        public static string Convert(string s, int numRows)
        {
            if (numRows==1) return s;

            StringBuilder str = new StringBuilder();
            int element = 2 * numRows - 2;

            Dictionary<int, string> dic = new Dictionary<int, string>();
            for (int i = 0; i < s.Length; i++)
            {
                int key = i / element;
                if (dic.ContainsKey(key)) dic[key] += s[i];
                else dic.Add(key, s[i].ToString());
            }

            //int numberOfCycles = s.Length / element;

            for (int i = 0; i < numRows; i++)
            {
                if (i == 0 || i == numRows - 1)
                {
                    foreach (var item in dic.Values)
                    {
                        if (i < item.Length) str.Append(item[i]);
                    }
                }
                else
                {
                    foreach (var item in dic.Values)
                    {
                        if (i < item.Length) str.Append(item[i]);
                        if (element - i < item.Length) str.Append(item[element - i]);

                    }
                }
            }
            return str.ToString();
        }
    }
}
